package com.unproy.model.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;


@Entity
@Table(name = "TBSCTG")
public class SubCategory implements Serializable, Comparable<SubCategory> {
	private static final long	serialVersionUID	= -2554739027756910306L;
	private Short				subCategoryId;
	private Category			parentCategory;
	private String				description;
	private Set<Article>		articles			= new HashSet<Article>();
	
	@Id
	@SequenceGenerator(name = "subCategoryId", sequenceName = "SQSCTG")
	@GeneratedValue(generator = "subCategoryId", strategy = GenerationType.SEQUENCE)
	@Column(name = "SCTGID", unique = true, nullable = false)
	public Short getSubCategoryId() {
		return this.subCategoryId;
	}
	
	public void setSubCategoryId(Short subCategoryId) {
		this.subCategoryId = subCategoryId;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PCTGID", nullable = false)
	public Category getParentCategory() {
		return this.parentCategory;
	}
	
	public void setParentCategory(Category category) {
		this.parentCategory = category;
	}
	
	@Column(name = "DSCRPT", nullable = false)
	public String getDescription() {
		return this.description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "subCategory")
	public Set<Article> getArticles() {
		return this.articles;
	}
	
	public void setArticles(Set<Article> articles) {
		this.articles = articles;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((this.description == null) ? 0 : this.description.hashCode());
		result = prime * result + ((this.subCategoryId == null) ? 0 : this.subCategoryId.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SubCategory other = (SubCategory) obj;
		if (this.description == null) {
			if (other.description != null)
				return false;
		} else if (!this.description.equals(other.description))
			return false;
		if (this.subCategoryId == null) {
			if (other.subCategoryId != null)
				return false;
		} else if (!this.subCategoryId.equals(other.subCategoryId))
			return false;
		return true;
	}
	
	@Override
	public int compareTo(SubCategory subCategory) {
		return getDescription().compareTo(subCategory.getDescription());
	}
	
	@Override
	public String toString() {
		return getDescription();
	}
}
